<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">登录</h2>
      <el-form :model="loginForm" ref="loginForm" class="login-form" @submit.native.prevent="login">
        <!-- 用户名输入框 -->
        <el-form-item>
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="el-icon-user"
            class="login-input"
          ></el-input>
        </el-form-item>

        <!-- 密码输入框 -->
        <el-form-item>
          <el-input
            v-model="loginForm.password"
            :type="passwordVisible ? 'text' : 'password'"
            placeholder="密码"
            prefix-icon="el-icon-lock"
            class="login-input"
          >
            <template #suffix>
              <el-button
                icon="el-icon-view"
                @click="togglePassword"
                class="password-toggle-btn"
                type="text"
              ></el-button>
            </template>
          </el-input>
        </el-form-item>

        <!-- 登录按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            class="login-button"
            @click="login"
            size="large"
            round
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "", // 用户名
        password: "", // 密码
      },
      passwordVisible: false, // 密码是否可见
    };
  },
  methods: {
    // 切换密码可见状态
    togglePassword() {
      this.passwordVisible = !this.passwordVisible;
    },

    // 显示消息提示
    showMessage(message, type = "success") {
      this.$message({
        message,
        type,
        duration: 2000,
      });
    },

    // 登录逻辑
    async login() {
      const { username, password } = this.loginForm;

      // 校验输入
      if (!username || !password) {
        this.showMessage("用户名或密码不能为空", "warning");
        return;
      }

      // 发送登录请求
      const response = await this.$request.post("/auth/api-token-auth/", {
        username,
        password,
      });
      console.log(response);

      // 处理响应
      if (response.code === 200) {
        this.showMessage("登录成功！正在跳转...", "success");
        localStorage.setItem("token", response.data.token); // 保存 token
        localStorage.setItem("is_admin",response.data.is_admin)
        this.$router.push("/tpa/home"); // 跳转到首页
      } else {
        this.showMessage("登录失败，请检查用户名或密码", "error");
      }
    },
  },
};
</script>

<style scoped>
/* 全局容器 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #e0eafc, #cfdef3);
  padding: 20px;
}

/* 登录框 */
.login-box {
  background: white;
  padding: 40px 50px;
  border-radius: 15px;
  box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  text-align: center;
  animation: fadeIn 0.5s ease-in-out;
}

/* 标题 */
.login-title {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin-bottom: 30px;
}

/* 表单 */
.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* 密码切换按钮 */
.password-toggle-btn {
  font-size: 18px;
  color: #909399;
  cursor: pointer;
}

.password-toggle-btn:hover {
  color: #007bff;
}

/* 登录按钮 */
.login-button {
  width: 100%;
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>